package com.zzh.jetpackdemo.paging

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zzh.jetpackdemo.R


class PagingActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_paging)
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        recyclerView.layoutManager = GridLayoutManager(this, 7)
        val config = PagedList.Config.Builder()
            .setPageSize(42)
            .setEnablePlaceholders(false)
            .setInitialLoadSizeHint(42)
            .build()
        val liveData: LiveData<PagedList<Int>> =
            LivePagedListBuilder<Int, Int>(CalendarDataSourceFactory(), config).build()
        val adapter = CalendarAdapter()
        recyclerView.adapter = adapter
        liveData.observe(this, object : Observer<PagedList<Int>> {

            override fun onChanged(t: PagedList<Int>?) {
                adapter.submitList(t)
            }
        })
    }

}

